package com.example.fw;

import java.util.List;
import java.util.Random;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends HelperBase {
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	private SortedListOf<ContactData> cachedContacts;
	
	public SortedListOf<ContactData> getContacts() {
		if (cachedContacts == null) {
			rebuildCache();
		}
		  return cachedContacts;
	}
		
	private void rebuildCache() {
		cachedContacts = new SortedListOf<ContactData>();
		manager.navigateTo().mainPage();
		List<WebElement> rows = getContactRows();
		  for (WebElement row : rows) {
			ContactData contact = new ContactData();
			contact.lastName = getLastNameInRow(row);
			cachedContacts.add(contact);
		}
	}

	public ContactHelper createContact(ContactData contact) {
		manager.navigateTo().mainPage();
		initContactCreation();
		fillContactForm(contact, CREATION);
		submitContactCreation();
		returnToMainPage();
		rebuildCache();
		return this;
	} 
	
	public ContactHelper modifyContact(ContactData contact, int index) {
		manager.navigateTo().mainPage();
		editContact(index);
		fillContactForm(contact, MODIFICATION);
		submitContactModification();
		returnToMainPage();
		rebuildCache();
		return this;
	}
	
	public ContactHelper removeContact(int index) {
		editContact(index);
		deleteContact();
		returnToMainPage();
		rebuildCache();
		return this;
	}
	
	//-----------------------------------------------------------------------------------------------------
	
	public ContactHelper returnToMainPage() {
		click(By.linkText("home page"));
		return this;
	}

	public ContactHelper initContactCreation() {
		click(By.linkText("add new"));
		return this;
	}

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstName());
		type(By.name("lastname"), contact.getLastName());
		type(By.name("address"), contact.getAddress());
		type(By.name("home"), contact.getHomePhone());
		type(By.name("mobile"), contact.getMobilePhone());
		type(By.name("work"), contact.getWorkPhone());
		type(By.name("email"), contact.getEmail());
		type(By.name("email2"), contact.getEmail2());
		select(By.name("bday"), contact.getBirthDay());
		selectByNumber(By.name("bmonth"), contact.getBirthMonth());
		type(By.name("byear"), contact.getBirthYear());
		if (formType == CREATION) {
			select(By.name("new_group"), contact.group = selectRandomGroup());
		} else {
				if (driver.findElements(By.name("new group")).size() !=0) {
					throw new Error("Group selector exists on the contact modification form");
				}
			}
		
		type(By.name("address2"), contact.getAddress2());
		type(By.name("phone2"), contact.getHomePhone2());
		return this;
	}
	
	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		return this;
	}
	
	public ContactHelper editContact(int index) {
		editContactByIndex(index);
		return this;
	}

	private void editContactByIndex(int index) {
		click(By.xpath("//tr[@name='entry'][" + (index+1) + "]/td[7]/a/img"));
	}

	public ContactHelper submitContactModification() {
		click(By.xpath("//form[1]/input[11]"));
		return this;
	}
	
	public ContactHelper deleteContact() {
		click(By.xpath("//form[2]/input[2]"));
		return this;
	}
	
	private String getLastNameInRow(WebElement row) {
		  return row.findElement(By.xpath("./td[2]")).getText();
	}	
	
	private List<WebElement> getContactRows() {
		return getElements(By.xpath("//tr[@name='entry']"));
	}

	public String selectRandomGroup() {
		List<WebElement> options = getElements(By.xpath("//form/select[3]/option"));
		Random rnd = new Random();
		return options.get(rnd.nextInt(options.size())).getText();
	}
}
	

